package com.lecture.java8.baidu;

import com.baidu.aip.speech.AipSpeech;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import org.json.JSONObject;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Sample {
    //设置APPID/AK/SK
    public static final String APP_ID = "6675081";
    public static final String API_KEY = "Y9RXeK7WbvldJkwFhrFAwu1L";
    public static final String SECRET_KEY = "FgFlZ3rucHeGZlWHvvpp5hwYDEjyNMIZ";

    public static void main(String[] args) {
        // 初始化一个AipSpeech
        AipSpeech client = new AipSpeech(APP_ID, API_KEY, SECRET_KEY);
        // 调用接口
        JSONObject res = client.asr("D:\\study\\tmp\\2025-04-05\\16k-23850.amr", "amr", 16000, null);



        System.out.println(res.toString(2));
        System.out.println("--------------------");
        Map<String, Object> map = res.toMap();
        Object o = map.get("result");
        List<String> list = (List<String>) o;
        // 如果你确定是 ArrayList，可以再转：
        ArrayList<String> arrayList = (ArrayList<String>) list;
        String result = String.join(",", arrayList);
        System.out.println(result);

        int lastDotIndex = result.lastIndexOf('.');
        if (lastDotIndex > 0) {
            String fileType = result.substring(lastDotIndex + 1);
            System.out.println("文件类型: " + fileType); // 输出: pdf
        } else {
            System.out.println("无法确定文件类型");
        }
    }

    /**
     *  mp3转pcm
     * @param mp3filepath MP3文件存放路径
     * @param pcmfilepath pcm文件保存路径
     * @return
     */
    public static boolean convertMP32Pcm(String mp3filepath, String pcmfilepath){
        try {
            //获取文件的音频流，pcm的格式
            AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
            //将音频转化为  pcm的格式保存下来
            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获得pcm文件的音频流
     * @param mp3filepath
     * @return
     */
    private static AudioInputStream getPcmAudioInputStream(String mp3filepath) {
        File mp3 = new File(mp3filepath);
        AudioInputStream audioInputStream = null;
        AudioFormat targetFormat = null;
        try {
            AudioInputStream in = null;
            MpegAudioFileReader mp = new MpegAudioFileReader();
            in = mp.getAudioInputStream(mp3);
            AudioFormat baseFormat = in.getFormat();
            targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                    baseFormat.getChannels(), baseFormat.getChannels()*2, baseFormat.getSampleRate(), false);
            audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return audioInputStream;
    }
}
